package b6.vacancyshare.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBLowongan
{
	public static final String KEY_IDLOWONGAN = "idlowongan";
    public static final String KEY_TITLE = "title";
    public static final String KEY_POSISI = "posisi";
    public static final String KEY_GAJI = "gaji";
    public static final String KEY_LOKASI= "lokasi";
    public static final String KEY_NAMAPERUSAHAAN = "namaperusahaan";
    public static final String KEY_DUEDATE = "duedate";
    public static final String KEY_SOFTSKILL = "softskill";
    public static final String KEY_TECHNICALSKILL = "technicalskill";
    
    private static final String TAG = "DBLowongan";
    
    private static final String DATABASE_NAME = "vacancy-share";
    private static final String DATABASE_TABLE_LOWONGANPEKERJAAN = "lowonganpekerjaan";
    private static final int DATABASE_VERSION = 1;
    
    private static final String DATABASE_CREATE_LOWONGANPEKERJAAN =
        "create table LOWONGANPEKERJAAN(idlowongan integer primary key autoincrement, "
        + "title text not null, posisi text not null , "
        + "gaji integer not null, lokasi text not null , "
        + "namaperusahaan text not null, duedate text not null , "
        + "softskill text, technicalskill text );";
    
private final Context context; 
    
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBLowongan(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
        
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE_LOWONGANPEKERJAAN);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS lowonganpekerjaan");
            onCreate(db);
        }
    }    
    
  //---opens the database---
    public DBLowongan open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }
    
  //---insert a Lowongan into the database---
    public long insertLowonganPekerjaan(String title, String posisi, int gaji, String lokasi, String namaperusahaan, String duedate, String softskill, String technicalskill) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_POSISI, posisi);
        initialValues.put(KEY_GAJI, gaji);
        initialValues.put(KEY_LOKASI, lokasi);
        initialValues.put(KEY_NAMAPERUSAHAAN, namaperusahaan);
        initialValues.put(KEY_DUEDATE, duedate);
        initialValues.put(KEY_SOFTSKILL, softskill);
        initialValues.put(KEY_TECHNICALSKILL, technicalskill);
        return db.insert(DATABASE_TABLE_LOWONGANPEKERJAAN, null, initialValues);
    }    
    
    public boolean deleteLowonganPekerjaan(long idlowongan) 
    {
        return db.delete(DATABASE_TABLE_LOWONGANPEKERJAAN, KEY_IDLOWONGAN + 
        		"=" + idlowongan, null) > 0;
    }
    
  //---retrieves all the lowonganpekerjaan---
    public Cursor getAllLowonganPekerjaan() 
    {
        return db.query(DATABASE_TABLE_LOWONGANPEKERJAAN, new String[] {
        		KEY_IDLOWONGAN, 
        		KEY_TITLE,
        		KEY_POSISI,
        		KEY_GAJI,
        		KEY_LOKASI,
        		KEY_NAMAPERUSAHAAN,
        		KEY_DUEDATE,
        		KEY_SOFTSKILL,
        		KEY_TECHNICALSKILL}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }
    
  //---retrieves a particular lowonganpekerjaan---
    public Cursor getLowonganPekerjaan(long idlowongan) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_LOWONGANPEKERJAAN, new String[] {
                		KEY_IDLOWONGAN,
                		KEY_TITLE, 
                		KEY_POSISI,
                		KEY_GAJI,
                		KEY_LOKASI, 
                		KEY_NAMAPERUSAHAAN,
                		KEY_DUEDATE, 
                		KEY_SOFTSKILL,
                		KEY_TECHNICALSKILL
                		}, 
                		KEY_IDLOWONGAN + "=" + idlowongan, 
                		null,
                		null, 
                		null,
                		null,
                		null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    
    //---updates a LowonganPekerjaan---
    public boolean updateLowonganPekerjaan(long idlowongan, String title, String posisi, int gaji,
    String lokasi, String namaperusahaan, String duedate, String softskill, String technicalskill) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_POSISI, posisi);
        args.put(KEY_GAJI, gaji);
        args.put(KEY_LOKASI, lokasi);
        args.put(KEY_NAMAPERUSAHAAN, namaperusahaan);
        args.put(KEY_DUEDATE, duedate);
        args.put(KEY_SOFTSKILL, softskill);
        args.put(KEY_TECHNICALSKILL, technicalskill);
        return db.update(DATABASE_TABLE_LOWONGANPEKERJAAN, args, 
                         KEY_IDLOWONGAN + "=" + idlowongan, null) > 0;
    }
    
    
}
